package org.shj.book2.chapter05;

public class Table {

	private final String[] buffer;
	private int tail;
	private int head;
	private int count;
	
	public Table(int count){
		this.buffer = new String[count];
		this.head = 0;
		this.tail = 0;
		this.count = 0;
	}
	
	public synchronized void put(String cake)throws InterruptedException{
		System.out.println(Thread.currentThread().getName() + " want to puts " + cake);
		while(count >= buffer.length){
			System.out.println("Table is full. " + Thread.currentThread().getName() + " wait().");
			wait();
		}
		
		System.out.println(Thread.currentThread().getName() + " puts " + cake);
		buffer[tail] = cake;
		tail = (tail + 1) % buffer.length;
		count++;
		notifyAll();
	}
	
	public synchronized String take() throws InterruptedException{
		while(count <= 0){
			System.out.println("There is no cake. " + Thread.currentThread().getName() + " wait()." );
			wait();
		}
		String cake = buffer[head];
		head = (head + 1) % buffer.length;
		count--;
		notifyAll();
		System.out.println(Thread.currentThread().getName() + " takes " + cake);
		return cake;
	}
}
